package impl;

import java.math.BigDecimal;

import java.util.Date;
import java.util.List;

import api.ejb.IWallet;
import api.ejb.IWallet.MovementsType;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;


import org.jboss.logging.Logger;

import p3_entities.Balance;
import p3_entities.Lindenmovement;
import p3_entities.Lindenmovementstype;


/**
 * Session Bean implementation class Wallet
 */
@Stateless(name = "IWallet")
@WebService(endpointInterface="api.ejb.IWallet",serviceName="BankWS")
 //@TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW)
public class Wallet implements IWallet {
	@PersistenceContext(unitName = "BillingPU")
	EntityManager em;
	private static final Logger log = Logger.getLogger(Wallet.class);


	/**
	 * Default constructor.
	 */
	public Wallet() {

	}

	public boolean reloadLinden(int uid, int amount, MovementsType type) {
		log.info("Executing ---------->");


		// Update profile value
		Balance balance = em.find(Balance.class, uid);
		
		if(balance == null) {
			balance = new Balance();
			balance.setLindenEuros(new BigDecimal(0));
			balance.setUserId(uid);
			
		}
		
		balance.setLindenEuros(balance.getLindenEuros().add(
				new BigDecimal(amount)));
		em.persist(balance);

		// Add new user movement
		Lindenmovement userMovements = new Lindenmovement();
		userMovements.setBalance(balance);
		userMovements.setLindenEuros(new BigDecimal(amount));
		userMovements.setLindenmovementstype((Lindenmovementstype) em.find(
				Lindenmovementstype.class, type.ordinal()));
		userMovements.setDate(new Date());
		em.persist(userMovements);
		log.info("<--------------- Done");


		return true;

	}

	public List<Lindenmovement> getMovements(int uid) {
		log.info("Executing ---------->");


		Query query = em.createNamedQuery("getLindenMovements");
		query.setParameter("userId", uid);
		log.info("<--------------- Done");

		return query.getResultList();

	}

	public boolean debitLinden(Integer uid, Integer amount) {
		
		log.info("Executing ---------->");


		// Update profile value
		Balance balance = em.find(Balance.class, uid);
		BigDecimal current = balance.getLindenEuros();
		balance.setLindenEuros(current.subtract(new BigDecimal(amount)));
		
		
		em.persist(balance);

		// Add new user movement
		Lindenmovement userMovements = new Lindenmovement();
		userMovements.setBalance(balance);
		userMovements.setLindenEuros(new BigDecimal(amount));
		userMovements.setLindenmovementstype((Lindenmovementstype) em.find(
				Lindenmovementstype.class, MovementsType.BET.ordinal()));
		userMovements.setDate(new Date());
		em.persist(userMovements);
		
		log.info("<--------------- Done");
		
		return true;
		
		
	}

	public boolean checkOperation( int uid, int amount) {
	
		log.info("Executing ---------->");

		// Update profile value
		Balance balance = em.find(Balance.class, uid);
		BigDecimal current = balance.getLindenEuros();
		
		log.info("<--------------- Done");

		
		if(current.doubleValue() >= amount)
			return true;
		else
			return false;
				
	}

	public Balance getUserBalance(int uid) {
		log.info("Executing ---------->");

		Balance b = em.find(Balance.class, uid);
		if(b == null)
		{
			b = new Balance();
			b.setUserId(uid);
			b.setLindenEuros(new BigDecimal(0));
			em.persist(b);
		}	
		b = em.find(Balance.class, uid);
		
		
		log.info("<--------------- Done");

		
		return b;
		
		
	}

}
